﻿@model int?

@using SmartStore.Web.Framework.UI;
@using SmartStore.Utilities;

@functions {
	private bool TransientUpload
	{
		get
		{
			if (ViewData.ContainsKey("transientUpload"))
				return ViewData["transientUpload"].Convert<bool>();
			return false;
		}
	}

	private bool ValidatePicture
	{
		get
		{
			if (ViewData.ContainsKey("validate"))
				return ViewData["validate"].Convert<bool>();
			return true;
		}
	}

	private bool Compact
	{
		get
		{
			return ViewData["compact"].Convert<bool?>() == true;
		}
	}
}

@{
	var random = CommonHelper.GenerateRandomInteger();
	var pictureService = this.CommonServices.PictureService;
	var mediaSettings = this.CommonServices.Resolve<SmartStore.Core.Domain.Media.MediaSettings>();
	int pictureId = Model.HasValue ? Model.Value : 0;
	var picture = pictureService.GetPictureById(pictureId);
}

<div class="fileupload-container">
	<div class="fileupload-thumb-stage">
		<div class="fileupload-thumb" style="background-image: url('@Url.Picture(pictureId, mediaSettings.ProductThumbPictureSize)')">
			@Html.HiddenFor(x => x, new { @class = "hidden" })
			@*<img src="@Url.Picture(pictureId, mediaSettings.ProductThumbPictureSize)" style="max-width: 64px; max-height: 64px" class="yimg-thumbnail" alt="" />*@
		</div>
	</div>

	<div class="fileupload-controls">
		@(Html.SmartStore().FileUploader()
            .UploadUrl(Url.Action("AsyncUpload", "Picture", new { isTransient = TransientUpload, validate = ValidatePicture, area = "Admin" }))
            .HtmlAttribute("data-fallback-url", pictureService.GetFallbackUrl(mediaSettings.ProductThumbPictureSize))
            .AcceptedFileTypes("gif|jpe?g|png")
            .ShowRemoveButton(picture != null)
            .ShowRemoveButtonAfterUpload(true)
            .Compact(ViewData["compact"].Convert<bool?>() == true)
            .OnUploadCompletedHandlerName("onUploadCompleted_" + random)
		)
	</div>
</div>

<script>
	window['onUploadCompleted_@random'] = function (e, el, data) {
		$("#add-product-picture").removeAttr('disabled');
	}
	window['onFileRemove_@random'] = function (e, el) {
		$("#add-product-picture").attr('disabled', 'disabled');
	}
	@*window['onAborted_@random'] = function (el) {
		//console.log("onAborted", arguments);
	}
	window['onError_@random'] = function (el, textStatus, errorThrown) {
		//console.log("onError", arguments);
	}*@
</script>